package br.unisinos.arquivos;

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.ArrayList;

import br.lp2p.commons.LP2PInternalMessages;

public class DirectoryEntry {
	private String dirName;
	private ArrayList<FileShared> files;
	private int numFiles = 0;

	public DirectoryEntry(String dirName) {
		this.dirName = dirName;

		files = new ArrayList<FileShared>();
	}

	public String getShareName() {
		return dirName;
	}

	public void setShareName(String shareName) {
		this.dirName = shareName;
	}

	public String getRelativeDirName() {
		String relativeName = null;
		int idx = dirName.lastIndexOf("/");

		if (idx != -1) {
			relativeName = dirName.substring(idx + 1);
		} else
			relativeName = dirName;

		return relativeName;
	}

	public int getNumFiles() {
		return numFiles;
	}

	public void setNumFiles(int numFiles) {
		this.numFiles = numFiles;
	}

	/*
	 * read all the information in the files present in the share
	 */
	public boolean readShare() {
		if (dirName == null)
			return false;

		try {
			FileSystem fs = FileSystems.getDefault();
			Path dirPath = fs.getPath(dirName);

			if (dirPath != null) {
				FileAttributeRetriever attributes= new FileAttributeRetriever(dirPath);

				if (Files.exists(dirPath) && attributes.isDirectory()) {
					DirectoryStream<Path> dirStream = Files.newDirectoryStream(dirPath);// dirPath.newDirectoryStream();

					for (Path file : dirStream) {
						
						//PosixFileAttributes fileAttrs = Files.getFileAttributeView(file, PosixFileAttributeView.class).readAttributes();//file.getFileAttributeView(PosixFileAttributeView.class).readAttributes();
						FileAttributeRetriever fileAttr = new FileAttributeRetriever(file);
						
						if (fileAttr.isRegularFile()) {
							FileShared sf = new FileShared(file);
							files.add(sf);
							numFiles++;
							 System.out.println("Name: " + sf.getName()
							 + " Id: " + sf.getId());
						}

					}

					return true;
				}
			}
		} catch (NoSuchFileException nsfe) {
			// System.out.println("Directory not found");
			nsfe.printStackTrace();
		} catch (IOException ioex) {
			// System.out.println("IO error");
			ioex.printStackTrace();
		}
		return false;
	}

	/*
	 * Search for a specific file in the share
	 */
	public FileShared getSelectedFile(long fileID) {
		for (FileShared sf : files) {
			if (sf.getId() == fileID)
				return sf;
		}

		return null;
	}

	public FileShared getSelectedFile(String name) {
		for (FileShared sf : files) {
			if (name.equals(sf.getName()))
				return sf;
		}

		return null;
	}

	public String getFormatedShare() {
		StringBuilder output = new StringBuilder();

		// output.append(numFiles);
		// output.append(LP2PInternalMessages.DELIM);
		for (FileShared sf : files) {

			output.append(sf.getId());
			output.append(LP2PInternalMessages.DELIM + sf.getName() + LP2PInternalMessages.DELIM + sf.getSize() + LP2PInternalMessages.DELIM + sf.getTimestamp() + LP2PInternalMessages.DELIM);
		}

		return output.toString();
	}

}
